package com.xteam.xnetworkdisk.sys.controller;

import com.xteam.xnetworkdisk.sys.dao.FileDao;
import com.xteam.xnetworkdisk.sys.dao.UserDao;
import com.xteam.xnetworkdisk.sys.entity.FileEntity;
import com.xteam.xnetworkdisk.utils.*;
import com.xteam.xnetworkdisk.utilsinterface.ChunkFileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity;

import java.io.InputStream;

@RestController()
@RequestMapping("email")
public class EmailController {

    @Autowired
    private EmailUtil emailUtil; // 假设你已经创建了 EmailService

    @Autowired
    private FileDao fileDao;

    @Autowired
    private FileLocalUtils fileLocalUtils;
    @Autowired
    private UserDao userDao;

    /**
     * 发送邮件
     * @param fileId
     * @return
     */
    @GetMapping("/sendEmail")
    public ResponseEntity<String> sendEmail(@RequestParam Integer fileId) {
        FileEntity fileEntity = fileDao.selectById(fileId);
        String filePath = fileEntity.getFilePath();
        Integer fileType = fileEntity.getFileType();
        String fileName = fileEntity.getFileName();
        InputStream fileInputStreamFromDisplayPath = fileLocalUtils.getFileInputStreamFromDisplayPath(filePath);

        try {

            emailUtil.sendEmail("2151636434@qq.com", "骁合云盘传输：" + fileName, fileInputStreamFromDisplayPath, fileType, fileName);
            return ResponseEntity.ok("Email sent successfully!");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body("Failed to send email: " + e.getMessage());
        }
    }
}
